<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="纸张编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入纸张编码"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="品牌" prop="brand">
        <el-select
          @keyup.enter="handleQuery"
          v-model="queryParams.brand"
          filterable
          class="m-2"
          placeholder="请选择纸张品牌"
          style="width: 200px"
        >
          <el-option
            v-for="item in mes_paper_brand"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纸张名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入纸张名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="定量" prop="weight">
        <el-input
          type="number"
          v-model="queryParams.weight"
          placeholder="请输入定量"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specs">
        <el-select
          @keyup.enter="handleQuery"
          v-model="queryParams.specs"
          class="m-2"
          filterable
          placeholder="请选择"
          style="width: 200px"
        >
          <el-option
            v-for="item in mes_paper_specs"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="幅宽" prop="longWide">
        <el-input
          v-model.trim="queryParams.longWide"
          clearable
          style="width: 200px"
          placeholder="请输入幅宽"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Upload" @click="handleImport"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="paperList">
      <el-table-column label="序号" width="60" type="index" align="center">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="mes_people_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="纸张编码" width="180" align="center" prop="code">
        <template v-slot="{ row }">
          <el-button link type="primary" @click="handleView(row)">{{
            row.code
          }}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="自有编码" align="center" prop="originalCode" /> -->
      <el-table-column
        label="纸张名称"
        min-width="180"
        align="center"
        prop="name"
      />
      <el-table-column
        label="幅宽"
        min-width="120"
        align="center"
        prop="longWide"
      />
      <!-- <el-table-column label="纸张全称" align="center" prop="fullName" min-width="200" /> -->
      <el-table-column label="规格型号" align="center" width="100" prop="specs">
        <template #default="scope">
          <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
        </template>
      </el-table-column>
      <el-table-column label="纸张品牌" align="center" prop="brand">
        <template #default="scope">
          <dict-tag :options="mes_paper_brand" :value="scope.row.brand" />
        </template>
      </el-table-column>
      <el-table-column label="计量单位" align="center" prop="unit">
        <template #default="scope">
          <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="采购专员" align="center" prop="buyerName" /> -->
      <el-table-column label="客/厂料" align="center" prop="material">
        <template #default="scope">
          <dict-tag :options="mes_paper_material" :value="scope.row.material" />
        </template>
      </el-table-column>
      <el-table-column label="用纸类型" align="center" prop="useType">
        <template #default="scope">
          <dict-tag :options="mes_paper_usetype" :value="scope.row.useType" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="packageType">
        <template #default="scope">
          <dict-tag
            :options="mes_paper_package"
            :value="scope.row.packageType"
          />
        </template>
      </el-table-column>
      <el-table-column label="纸型" align="center" prop="paperType">
        <template #default="scope">
          <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customName" />
      <el-table-column label="库存数量" align="center" prop="quantity" />
      <!-- <el-table-column label="定量" align="center" prop="weight" />
            <el-table-column label="幅宽(mm)" width="100" align="center" prop="longWide" /> -->
      <!-- <el-table-column label="宽度(mm)" align="center" prop="wide" /> -->
      <!-- <el-table-column label="高度(mm)" align="center" prop="height" /> -->
      <!-- <el-table-column label="安全库存" align="center" prop="safetyStock" /> -->

      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        min-width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            content="编辑"
            placement="top"
            v-if="scope.row.status == 1"
          >
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
            ></el-button>
          </el-tooltip>
          <!--<el-tooltip content="删除" placement="top" v-if="scope.row.status == 1">
                        <el-button link type="primary" icon="Delete" @click="handleDle(scope.row)"></el-button>
                    </el-tooltip> -->
          <el-button
            link
            type="primary"
            icon="TurnOff"
            @click="handleServe(scope.row)"
            >{{ scope.row.status == 0 ? "停用" : "启用" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      style="margin-top: 0; padding-top: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <div class="el-upload__tip">
              <el-checkbox
                v-model="upload.updateSupport"
              />是否更新已经存在的用户数据
            </div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link
              type="primary"
              :underline="false"
              style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate"
              >下载模板</el-link
            >
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup name="customer">
import {
  listPaper,
  delPaper,
  changeMesPaperStatus,
  codeExport,
  codeTemplate,
} from "@/api/basicInfo/paperCoding.js";
import useTagsViewStore from "@/store/modules/tagsView";
const tagsViewStore = useTagsViewStore();
const { proxy } = getCurrentInstance();
const {
  mes_paper_brand,
  mes_paper_unit,
  mes_paper_material,
  mes_paper_usetype,
  mes_paper_package,
  mes_paper_type,
  mes_paper_specs,
  mes_people_status,
} = proxy.useDict(
  "mes_paper_brand",
  "mes_paper_unit",
  "mes_paper_material",
  "mes_paper_usetype",
  "mes_paper_package",
  "mes_paper_type",
  "mes_paper_specs",
  "mes_people_status"
);
import { downLoadBlob } from "@/utils/ruoyi";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const route = useRoute();
const paperList = ref([]);
const loading = ref(false);
const total = ref(0);

const showSearch = ref(true);

import { getToken } from "@/utils/auth";
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/mes/paper/importData",
});
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    name: null,
  },
  rules: {
    code: [{ required: true, message: "客户编码不能为空", trigger: "blur" }],
    // name: [
    //     { required: true, message: "客户名称不能为空", trigger: "blur" }
    // ],
  },
});
const { queryParams, form, rules } = toRefs(data);
if (route.query.code) {
  queryParams.value.code = route.query.code;
  listPaper(queryParams.value).then((res) => {
    if (res.rows.length > 0) {
      tagsViewStore.delView(route);
      handleView(res.rows[0]);
    }
  });
} else {
  getList();
}
// 查看
const handleView = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      id: row.id,
      status: row.status,
      code: row.code,
      readonly: true,
    },
  });
};
// 新增
const handleAdd = () => {
  router.push("/basicInfo/paperCoding/details");
};
const handleEdit = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      id: row.id,
      status: row.status,
      code: row.code,
    },
  });
};
//停用启用
const handleServe = (row) => {
  const state = row.status === 0 ? "1" : "0";
  const stateText = row.status === 0 ? "停用" : "启用";
  ElMessageBox.confirm(
    `确定要 ${stateText} 名称为 ${row.code} 的纸张编码吗?`,
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(() => {
    // 用户点击了确定按钮
    changeMesPaperStatus(row.id, state).then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: `${stateText}成功`,
          type: "success",
        });
        getList();
      }
    });
  });
};

//删除
const handleDle = (row) => {
  ElMessageBox.confirm(
    "确定要删除编码为 " + row.code + " 的纸张编码吗?",
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      // 用户点击了确定按钮
      delPaper(row.id).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          getList();
        }
      });
    })
    .catch(() => {
      //取消
    });
};
function getList() {
  loading.value = true;
  listPaper(queryParams.value)
    .then((response) => {
      paperList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.code = null;
  queryParams.value.name = null;
  queryParams.value.brand = null;
  proxy.resetForm("queryRef");
  handleQuery();
}
// 导出
const handleExport = () => {
  codeExport(queryParams.value).then((res) => {
    downLoadBlob(res, "纸张编码列表");
  });
};
/** 导入按钮操作 */
function handleImport() {
  upload.title = "纸张编码导入";
  upload.open = true;
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert(
    "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
      response.msg +
      "</div>",
    "导入结果",
    { dangerouslyUseHTMLString: true }
  );
  getList();
};
/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
}
/** 下载模板操作 */
function importTemplate() {
  codeTemplate().then((res) => {
    downLoadBlob(res, "纸张编码模版");
  });
}
</script>
 